package com.kafka.model;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;

import java.util.concurrent.ConcurrentHashMap;

/**
 *  网络事件配置类
 */
public class TcpServerFactory extends ChannelInitializer<SocketChannel> {

    private TcpServerConfig tcpServerConfig;
    private ConcurrentHashMap<String, Channel> channelMap;

    public TcpServerFactory(TcpServerConfig tcpServerConfig, ConcurrentHashMap<String, Channel> channelMap){
        this.tcpServerConfig = tcpServerConfig;
        this.channelMap = channelMap;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //添加读超时控制,秒为单位
        pipeline.addLast(new ReadTimeoutHandler(tcpServerConfig.getTimeoutclient()));
        pipeline.addLast("decoder", new TcpServerDecoder(tcpServerConfig));
        pipeline.addLast("encoder", new TcpServerEncoder());
        pipeline.addLast("handler", new TcpServerHandler(tcpServerConfig, channelMap));
    }
}
